﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using DealKhuyenMaiV2.com.Models;
namespace DealKhuyenMaiV2.com.Helpers
{
    public class MyRoleProvider : RoleProvider
    {
        DKMDatabaseEntities dKMDataEntities = new DKMDatabaseEntities();
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            Account account = dKMDataEntities.Accounts.SingleOrDefault(acc => acc.Email == username);
            if(account==null)
                return new string[] { string.Empty };
            TypeAccount typeAccount = account.TypeAccount;
            if (typeAccount == null)
                return new string[] { string.Empty };
                 return new string[] { typeAccount.NameTypeAccount };
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string userName, string roleName)
        {
            Account account = dKMDataEntities.Accounts.SingleOrDefault(acc => acc.Email == userName);
            TypeAccount typeAccount = dKMDataEntities.TypeAccounts.SingleOrDefault(role => role.NameTypeAccount == roleName);
            /*User user = repository.GetUser(userName);
        Role role = repository.GetRole(roleName);
            
        if (!repository.UserExists(user))
            return false;
        if (!repository.RoleExists(role))
            return false;
            */
            if (account == null || typeAccount==null)
                return false;
            return account.TypeAccount.NameTypeAccount == typeAccount.NameTypeAccount;

        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}